home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / lcheck < prev    next >
Text File  |  1994-09-17  |  2KB  |  76 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Synopsis:   Check R-files for global variable references.
  4.  
  5. //  Syntax:    lcheck ( FR )
  6.  
  7. //  Description:
  8.  
  9. //  lcheck uses the built-in function, fvscope, to look for global
  10. //  variable references from within functions. Global variable
  11. //  references, from within functions are not necessarily
  12. //  bad. However, when writing a library of R-files for widespread
  13. //  usage it is best to avoid references to global variables, other
  14. //  than functions.
  15.  
  16. //  lcheck takes a single string argument, FR, which is the
  17. //  filename(s) of the R-files to be checked. FR will be expanded by
  18. //  the shell, so shell meta-characters are allowed.
  19.  
  20. //         lcheck ("*.r");
  21.  
  22. //  The above will check all the R-files (files with a suffix of `.r')
  23. //  with fvscope, prompting the user for a return between files.
  24.  
  25. //  Note: Do not execute this function on itself.
  26.  
  27. //  Dependencies:
  28. //     This file requires AWK, preferrable mawk.
  29.  
  30. //-------------------------------------------------------------------//
  31.  
  32. //
  33. // A simple AWK program
  34. //
  35.  
  36. static (fname_awk)
  37. fname_awk = "/=[ ]*function[ ]*\(*/ { fn = 1; print $1 }"+...
  38.             "END { if (!fn) { printf(\"NO_FUNCTION_NAMES...\\n\"); }}";
  39.  
  40. static (AWK)
  41. AWK = "mawk";
  42.  
  43. lcheck = function ( FR )
  44. {
  45.   local (ans, flist, i, fname, str1, str2, str3, str4)
  46.  
  47.   if (class (FR) != "string") 
  48.   {
  49.     error ("lcheck: requires a string argument");
  50.   }
  51.  
  52.   str1 = "| ls "+FR;
  53.   str3 = "| "+ AWK + " '/\*/'";
  54.  
  55.   while (length (ans = getline (str1)))
  56.   {
  57.     for (i in members (ans))
  58.     {
  59.       str2 = "| "+AWK+" '" + fname_awk + "' < "+ans.[i];
  60.       flist = getline (str2);
  61.       if (flist.[1] != "NO_FUNCTION_NAMES...")
  62.       {
  63.     fname = flist.[1];
  64.     printf ("Checking variable scope in: %s\n", fname);
  65.     load (ans.[i]);
  66.     str4 = "fvscope (" + fname + ",\"" + str3 + "\")";
  67.     eval (str4);
  68.     close (str3);
  69.     pause ();
  70.       }
  71.       close (str2);
  72.     }
  73.   }
  74.   close (str1);
  75. };
  76.